trigger UpdateCatalogFieldOnOppty on Opportunity (before insert, before update) {
    String recordTypeName=null;

    //get the oppty record type id and names
    Map<String, String> mapOpportunityRecordTypes = new Map<String, String>();
    Map<String,String> opptyRecordTypes = new Map<String,String>{};
    
    List<RecordType> lst_RecTypeOpportunity = [Select Id, Name, DeveloperName From RecordType Where SObjectType = 'Opportunity' 
                                        AND IsActive = true];
    for(RecordType rt : lst_RecTypeOpportunity){
        mapOpportunityRecordTypes.put(rt.Id, rt.DeveloperName); 
        opptyRecordTypes.put(rt.Id,rt.Name);
    }

    Map<String, String> mapOpportunityToCatalogRecordType = new Map<String, String>();
    
    List<OpportunityToCatalogRecordTypes__c> listCS = OpportunityToCatalogRecordTypes__c.getAll().values();
    
    System.debug('====Custom Settings size: ' + listCS.size());
    for(Integer nCount = 0; nCount < listCS.size(); nCount++){
        mapOpportunityToCatalogRecordType.put(listCS[nCount].OpportunityRecordType__c, listCS[nCount].CatalogRecordType__c);
    }

    /*get the catalog record type and its records */
    List<RecordType> ctRecTypeList=[SELECT Id, Name, SobjectType, IsActive, DeveloperName from RecordType where SobjectType='Catalog__c'];
    List<Catalog__c> ctList=[Select id, Name, recordTypeId, Campus__c, Program__c, RecordType.DeveloperName from Catalog__c];
    System.debug('====Catalog records: ' + ctList.size());
    
    // map catalog and lead record type
    Map<String, String> mapOpptyToCatalog = new Map<String, String>();
    mapOpptyToCatalog.put('ULA - Traditional Prospective Student', 'ULA - Traditional Catalog');
    mapOpptyToCatalog.put('ULA - Working Adults Prospective Student', 'ULA - Working Adults Catalog');
    System.debug('====Oppty To Catalog: ' + mapOpptyToCatalog);

//*******************************************
//STEP 2: LOOKUP MATCHING CRITERIA CHECK ENDS
//*******************************************
    for (Opportunity opptyRecord: System.Trigger.new) 
    {        
        
        if (mapOpportunityToCatalogRecordType.get(mapOpportunityRecordTypes.get(opptyRecord.RecordTypeId)) == null){
            continue;
        }
        String catalogRecordTypeName = mapOpportunityToCatalogRecordType.get(mapOpportunityRecordTypes.get((opptyRecord.RecordTypeId)));
        recordTypeName=opptyRecord.RecordTypeId;
        //getting the tenant short name values from the Tenants__c List Custom Setting    
        if (opptyRecord.Campus__c == null || opptyRecord.Program__c == null){
            opptyRecord.Catalog__c = null;
            opptyRecord.Payment_plan__c = '';
        }
        /*Catalog lookup id population*/               
        if((opptyRecord.Campus__c != null) && (opptyRecord.Program__c != null) && 
            (opptyRecord.StageName != 'Application Submitted') && (opptyRecord.StageName != 'Registered'))
        {           
            String strOpptyRecordTypeName = opptyRecord.recordTypeId;            
            String strCatalogRecordTypeName = catalogRecordTypeName;
            //Check if catalog record found
            Boolean bFound = false;
            for(Catalog__c ct : ctList)
            {
                if((ct.Campus__c == opptyRecord.Campus__c) && 
                    (ct.Program__c == opptyRecord.Program__c) &&
                   (ct.RecordType.DeveloperName == strCatalogRecordTypeName))
                {
                    opptyRecord.Catalog__c=null;
                    opptyRecord.Catalog__c=ct.Id;
                    bFound = true;
                    break;
                }
            }    
            if (bFound == false){
                opptyRecord.Catalog__c.addError('Invalid Campus and Program selection. No catalog found for selected Campus and Program.');
            }
        }    
        //************************************
        //STEP 2: LOOKUP CHECK ENDS HERE
        //************************************
    }
    //************************************
    //TRIGGER.NEW ENDS HERE
    //************************************
}//Trigger ends here